﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Jurvis
{
    class OCR
    {
        string OCR_Do(string fileName)
        {
            MODI.Document doc = new MODI.Document();
            StringBuilder sb = new StringBuilder();
            MODI.Image image;
            MODI.Layout layout;

            doc.Create(fileName);
            try
            {
                doc.OCR(MODI.MiLANGUAGES.miLANG_CHINESE_SIMPLIFIED, true, true);
                
                for (int i = 0; i < doc.Images.Count; i++)
                {
                    image = (MODI.Image)doc.Images[i];
                    layout = image.Layout;
                    sb.Append(layout.Text);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return sb.ToString();
        }

        static void ToGrey()
        {
            Bitmap img1 = new Bitmap("E:\\1.bmp", true);

            for (int i = 0; i < img1.Width; i++)
            {
                for (int j = 0; j < img1.Height; j++)
                {
                    Color pixelColor = img1.GetPixel(i, j);
                    //计算灰度值

                    int diff_rg = pixelColor.R - pixelColor.G;
                    int diff_rb = pixelColor.R - pixelColor.B;
                    int diff_gb = pixelColor.G - pixelColor.B;

                    if ((Math.Abs(diff_gb) < 5 || Math.Abs(diff_rb) < 5 || Math.Abs(diff_rg) < 5) && pixelColor.R > 0x90)
                    {
                        Color newColor = Color.FromArgb(0xff, 0xff, 0xff);
                        img1.SetPixel(i, j, newColor);
                    }
                    else
                    {
                        Color newColor = Color.FromArgb(0x00, 0x00, 0x00);
                        img1.SetPixel(i, j, newColor);
                    }
                }
            }

            img1.Save("E:\\5.bmp");
        }

        public void getPic()
        {
            Bitmap bit = new Bitmap(200, 200);
            Graphics g = Graphics.FromImage(bit);
            g.CopyFromScreen(new Point(0, 0), new Point(0,0), bit.Size);

            bit.Save("E:\\1.bmp");
        }
    }
}
